<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>interp</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 11/03/2005</div>
    <p>
      <b>interp</b> - cubic spline evaluation function</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[yp [,yp1 [,yp2 [,yp3]]]]=interp(xp, x, y, d [, out_mode])</tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>xp</b>
        </tt>: real vector or matrix</li>
      <li>
        <tt>
          <b>x,y,d</b>
        </tt>: real vectors of the same size defining a cubic spline or sub-spline function 
                (called <tt>
          <b>s</b>
        </tt> in the following)</li>
      <li>
        <tt>
          <b>out_mode</b>
        </tt>: (optional) string defining the evaluation of <tt>
          <b>s</b>
        </tt> outside the [x1,xn] interval</li>
      <li>
        <tt>
          <b>yp</b>
        </tt>: vector or matrix of same size than <tt>
          <b>xp</b>
        </tt>, elementwise evaluation of <tt>
          <b>s</b>
        </tt>
                on <tt>
          <b>xp</b>
        </tt> (yp(i)=s(xp(i) or yp(i,j)=s(xp(i,j))</li>
      <li>
        <tt>
          <b>yp1, yp2, yp3</b>
        </tt>: vectors (or matrices) of same size than <tt>
          <b>xp</b>
        </tt>, elementwise evaluation of the 
                successive derivatives of <tt>
          <b>s</b>
        </tt> on <tt>
          <b>xp</b>
        </tt>
      </li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    Given three vectors <tt>
        <b>(x,y,d)</b>
      </tt> defining a spline or sub-spline function
    (see <a href="splin.htm">
        <tt>
          <b>splin</b>
        </tt>
      </a>) with <tt>
        <b>yi=s(xi), di = s'(xi)</b>
      </tt> this function
    evaluates <tt>
        <b>s</b>
      </tt> (and <tt>
        <b>s', s'', s'''</b>
      </tt> if needed) at <tt>
        <b>xp(i)</b>
      </tt> :
    </p>
    <pre>
      yp(i) = s(xp(i))    or  yp(i,j) = s(xp(i,j))
      yp1(i) = s'(xp(i))   or  yp1(i,j) = s'(xp(i,j))
      yp2(i) = s''(xp(i))   or  yp2(i,j) = s''(xp(i,j))
      yp3(i) = s'''(xp(i))   or  yp3(i,j) = s'''(xp(i,j))
         </pre>
    <p>
    The <tt>
        <b>out_mode</b>
      </tt> parameter set the evaluation rule for extrapolation,
    i.e. for <tt>
        <b>xp(i)</b>
      </tt> not in [x1,xn] : 
    </p>
    <dd>
      <li>
        <b>
          <font color="maroon">"by_zero"</font>
        </b>: an extrapolation by zero is done</li>
      <li>
        <b>
          <font color="maroon">"by_nan"</font>
        </b>: extrapolation by Nan</li>
      <li>
        <b>
          <font color="maroon">"C0"</font>
        </b>: the extrapolation is defined as follows :<pre>
      s(x) = y1  for  x &lt; x1
      s(x) = yn  for  x &gt; xn
         </pre>
      </li>
      <li>
        <b>
          <font color="maroon">"natural"</font>
        </b>: the extrapolation is defined as follows (<em>p_i</em> being the polynomial defining 
               <tt>
          <b>s</b>
        </tt> on [x_i,x_{i+1}]) :<pre>
      s(x) = p_1(x)      for  x &lt; x1
      s(x) = p_{n-1}(x)  for  x &gt; xn
         </pre>
      </li>
      <li>
        <b>
          <font color="maroon">"linear"</font>
        </b>: the extrapolation is defined as follows :<pre>
      s(x) = y1 + s'(x1)(x-x1)  for  x &lt; x1
      s(x) = yn + s'(xn)(x-xn)  for  x &gt; xn
         </pre>
      </li>
      <li>
        <b>
          <font color="maroon">"periodic"</font>
        </b>: <tt>
          <b>s</b>
        </tt> is extended by periodicity.</li>
    </dd>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>
// see the examples of splin and lsq_splin

// an example showing C2 and C1 continuity of spline and subspline
a = -8; b = 8;
x = linspace(a,b,20)';
y = sinc(x);
dk = splin(x,y);  // not_a_knot
df = splin(x,y, "fast");
xx = linspace(a,b,800)';
[yyk, yy1k, yy2k] = interp(xx, x, y, dk); 
[yyf, yy1f, yy2f] = interp(xx, x, y, df); 
xbasc()
subplot(3,1,1)
plot2d(xx, [yyk yyf])
plot2d(x, y, style=-9)
legends(["not_a_knot spline","fast sub-spline","interpolation points"],...
        [1 2 -9], "ur",%f)
xtitle("spline interpolation")
subplot(3,1,2)
plot2d(xx, [yy1k yy1f])
legends(["not_a_knot spline","fast sub-spline"], [1 2], "ur",%f)
xtitle("spline interpolation (derivatives)")
subplot(3,1,3)
plot2d(xx, [yy2k yy2f])
legends(["not_a_knot spline","fast sub-spline"], [1 2], "lr",%f)
xtitle("spline interpolation (second derivatives)")

// here is an example showing the different extrapolation possibilities
x = linspace(0,1,11)';
y = cosh(x-0.5);
d = splin(x,y);
xx = linspace(-0.5,1.5,401)';
yy0 = interp(xx,x,y,d,"C0");
yy1 = interp(xx,x,y,d,"linear");
yy2 = interp(xx,x,y,d,"natural");
yy3 = interp(xx,x,y,d,"periodic");  
xbasc()
plot2d(xx,[yy0 yy1 yy2 yy3],style=2:5,frameflag=2,leg="C0@linear@natural@periodic")
xtitle(" different way to evaluate a spline outside its domain")
 </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="splin.htm">
        <tt>
          <b>splin</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="lsq_splin.htm">
        <tt>
          <b>lsq_splin</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Author</font>
    </h3>
    <p>
    B. Pincon
  </p>
  </body>
</html>
